package com.elysia.gulimall.product.web;

import com.elysia.gulimall.product.entity.CategoryEntity;
import com.elysia.gulimall.product.service.CategoryService;
import com.elysia.gulimall.product.vo.Level2CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 前缀     spring.thymeleaf.prefix=classpath:/templates/
     * 后缀     spring.thymeleaf.suffix=.html
     * 视图解析器 进行拼串
     * @return
     */
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){

        //获取 一级分类 ，并装入模型
        List<CategoryEntity> categoryEntityList = categoryService.getLevel1Category();
        model.addAttribute("categories",categoryEntityList);

        return "index";
    }

    @ResponseBody
    @GetMapping("index/catalog.json")
    public Map<String,List<Level2CategoryVo>> getCategories(){
        Map<String,List<Level2CategoryVo>> categoryMap = categoryService.getLevel2AndLevel3CategoryWithSpringCache();
        return categoryMap;
    }
}
